package sz.szsmk.citizencard.mvp.presenter;

import android.util.Log;

import com.google.gson.Gson;
import com.umeng.socialize.media.Base;

import org.json.JSONObject;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.request.QueryUserCardRequest;
import sz.szsmk.citizencard.model.network.bean.request.UnbindCardReq;
import sz.szsmk.citizencard.model.network.bean.request.YscReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.model.network.bean.response.YXCRes;
import sz.szsmk.citizencard.mvp.IBindCitizenCardMvpView;
import sz.szsmk.citizencard.mvp.contract.IRoutineCardContact;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * author : hyt
 * date : 2019/10/14
 * description :
 */
public class BindCitizenCardPresenter extends AppBasePresenter<IBindCitizenCardMvpView> {

    private NetworkDateSources mNetworkDateSource;

    @Inject
    public BindCitizenCardPresenter() {
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    public void queryUserCard(String origin) {
        QueryUserCardRequest cardRequest = new QueryUserCardRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        cardRequest.setAccess_token(access_token);
        cardRequest.setType("00");
        cardRequest.setOrigin(origin);
        String json = new Gson().toJson(cardRequest);
        Log.e("queryUserCard->",json);
        mNetworkDateSource.queryUserCardInfo(cardRequest, new SubscribeResult<QueryUserCardRes>() {
            @Override
            protected void onOk(QueryUserCardRes response) {
                String json = new Gson().toJson(response);
                //L.e(Constant.TAG, "queryUserCard----->" + json);
                L.e(Constant.TAG, "queryUserCard----->" + response.getData().toString());
                getMvpView().queryUserCardSuccess(response);

            }

            @Override
            protected void onFailure(QueryUserCardRes response) {
                super.onFailure(response);
                String warningMsg = response.getData().get(0).getWarningMsg();
                getMvpView().queryUserCardFalie(warningMsg);
                Log.i("warningMsginfo",warningMsg);
            }

            @Override
            protected void onNext(QueryUserCardRes response) {
                super.onNext(response);
                getMvpView().queryUserCardFalie(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    public void queryYCX(String cardNo) {
        YscReq yscRes = new YscReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        yscRes.setCardNo(cardNo);
        yscRes.setAccessToken(access_token);

        String json = new Gson().toJson(yscRes);

        mNetworkDateSource.ysc(yscRes, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                String json = new Gson().toJson(response);

                getMvpView().queryYCXSucc(response);

            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                //String warningMsg = response.getData().get(0).getWarningMsg();
               // getMvpView().queryUserCardFalie(warningMsg);

            }
//
//            @Override
//            protected void onNext(QueryUserCardRes response) {
//                super.onNext(response);
//                getMvpView().queryUserCardFalie(response.getMessage());
//            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });

    }

    //删除卡片
    public void unBindCard(String accessToken, String cardNo) {
        UnbindCardReq unbindCardReq = new UnbindCardReq();
        unbindCardReq.setAccess_token(accessToken);
        unbindCardReq.setCardNo(cardNo);
        String json = new Gson().toJson(unbindCardReq);
        L.e(Constant.TAG, "unBindCard---->" + json);
        mNetworkDateSource.unBindCard(unbindCardReq, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "unBindCard---->" + toJson);
                getMvpView().unBindCardSucc();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                getMvpView().unBindCardFail(response.getMessage());

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().unBindCardFail(throwable.getMessage());

            }
        });
    }
}
